package com.example.nna.voz;

import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import com.example.nna.main.R;
import com.example.nna.util.Subscribe;
import com.example.nna.util.UserInfo;

public class MyService extends Service {	
	int counter = 0;
	public URL[] urls;
	
	static final int UPDATE_INTERVAL = 1000;
	private Timer timer = new Timer();
	private Context mContext;
	private PaserTask mTask;
	private ParserUserCP mParser;
	private Subscribe mSubscribe;
	private UserInfo mUserInfo;
	public class MyBinder extends Binder {
		MyService getService() {
			
			return MyService.this;
		}
	}	
	
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
		//return binder;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// We want this service to continue running until it is explicitly    
		// stopped, so return sticky.
        //Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();        
        mContext = this;
        mUserInfo = new UserInfo();
        doSomethingRepeatedly();
		return START_STICKY;
	}	

	@Override
    public void onDestroy() {
        super.onDestroy();     
        if (timer != null){
        	timer.cancel();
        }
        //Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
    }

	private void doSomethingRepeatedly() {
		timer.scheduleAtFixedRate( new TimerTask() {
			public void run() {
		       
                try {
					Thread.sleep(Voz.mNotifTimer*60000);
	                Log.d("MyService", counter + " Finished");
	                Log.d("nna",String.valueOf(Voz.mNotifTimer)+";" 
	                +String.valueOf(Voz.mNotifEnable));
	                if(mUserInfo.isLogin() && (Voz.mNotifEnable != false)){
		                mTask = new PaserTask();
		  		        mTask.execute(Voz.PASER_PASER);
	                }
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}, 0, UPDATE_INTERVAL);
	}
	 public class PaserTask extends AsyncTask<Integer, Void, Integer>{

			@Override
			protected Integer doInBackground(Integer... params) {
				// TODO Auto-generated method stub
				mParser = new ParserUserCP("http://vozforums.com/usercp.php");
				return mParser.paserSubscribe();
			}
			@Override
			protected void onPreExecute() {
				// TODO Auto-generated method stub
				super.onPreExecute();
			}
			@Override
			protected void onPostExecute(Integer result) {
				
				if(result == 0){
					for(int ii=0;ii<mParser.getContain().size();ii++){
						if(mParser.getContain(ii).Thread() != null){
							Intent i = new Intent(mContext,NotificationView.class);
							Bundle extras = new Bundle();
							extras.putString("url",mParser.getContain(ii).Reply());
						 	extras.putString("forum","test");
						 	Log.d("nna", mParser.getContain(ii).Reply());
						 	i.putExtras(extras);
					        PendingIntent pi = PendingIntent.getActivity(mContext, 0, i, PendingIntent.FLAG_UPDATE_CURRENT );
					        NotificationManager mm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
					        Notification notif = new Notification(R.drawable.logovoz, "Vozforums", System.currentTimeMillis());
					        CharSequence from =mParser.getContain(ii).LastPost();
					        CharSequence message =mParser.getContain(ii).Thread();
					        notif.setLatestEventInfo(mContext, message, from, pi);
					        notif.defaults |= Notification.DEFAULT_SOUND;
					        //notif.defaults |= Notification.DEFAULT_VIBRATE;
					        mm.notify(2, notif);
					        break;
						}
						
					}

				}
			}
	     }	
	
}